<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */

namespace MoChat\App\Call\Action\Dashboard;

use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Call\Constants\ClientTypeCode;
use MoChat\App\Call\Logic\RecordIndexLogic;
use MoChat\App\Client\Model\Client;
use MoChat\App\Common\Middleware\DashboardAuthMiddleware;
use MoChat\App\Rbac\Middleware\PermissionMiddleware;
use MoChat\App\Client\Logic\IndexLogic;
use MoChat\App\Sales\Contract\CallRecordContract;
use MoChat\App\Sales\Contract\SalesClientTaskContract;
use MoChat\App\Sales\Model\CallRecord;
use Hyperf\Di\Annotation\Inject;
use MoChat\App\User\Model\User;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;
use MoChat\Framework\Request\ValidateSceneTrait;

/**
 * 呼叫记录新版详情.
 * @Controller
 */
class RecordInfo extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var CallRecordContract
     */
    protected CallRecordContract $callRecordService;

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class),
     *     @Middleware(PermissionMiddleware::class)
     * })
     *
     * @RequestMapping(path="/dashboard/call/recordInfo", methods="GET")
     */
    public function handle()
    {
        $user = user();
        if (empty($user)) {
            throw new CommonException(ErrorCode::AUTH_UNAUTHORIZED, '登入异常请重新登入');
        }
        $data = $this->request->all();
        $this->validated($data);
        $detail = Db::table('call_record as record')
            ->leftJoin('client as client', 'client.id', '=', 'record.client_id')
            ->leftJoin('user as user', 'user.id', '=', 'record.user_id')
            ->when(!empty($data['id']), function ($query) use ($data) {
                $query->where('record.id', '=', $data['id']);
            })
            ->select([
                'record.id',
                'client.enterprise_name as enterpriseName',//客户名称
                'record.over_time as overTime',//挂机时间
                'record.call_type as callType',//呼叫类型
                'user.extn',//线路
                'record.call_duration as callDuration',//通话时长
                'record.caller_id_number as callerIdNumber',//主叫号码
                'record.destination_number as destinationNumber',//被叫号码
                'record.created_time as createdTime',//呼叫时间
                'record.answered_time as answeredTime',//接听时间
                'record.wait_duration as waitDuration',//接听等待时长
            ])->first();
        $detail || $detail = collect([]);
        $detail = json_decode(json_encode($detail), true);
        $detail['callStatus'] = $detail['callDuration'] > 0 ? '接通' : '未接通';
        return $detail;
    }

    /**
     * 验证规则.
     *
     * @return array 响应数据
     */
    protected function rules(): array
    {
        return [
            'page' => 'integer',
            'perPage' => 'integer',
        ];
    }

    /**
     * 验证错误提示.
     * @return array 响应数据
     */
    protected function messages(): array
    {
        return [
            'page.integer' => '页码必须为整数',
            'perPage.integer' => '页码必须为整数',
        ];
    }
}
